package GUI;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import client.*;
import control.*;
import entities.User;
import java.awt.Dimension;
import javax.swing.JLabel;

import org.jfree.ui.RefineryUtilities;

import java.awt.Color;
import java.util.ArrayList;

@SuppressWarnings("serial")
/**
 * Shows the MainFrame of the running program
 * @author Benny
 *
 */
public class MainFrame extends JFrame {
	
	public LoginPanel lg;
	public SideBar sb;
	public ConnectionGUI conGUI;
	public LibrarianSideBarGUI lsb;
	public UserSideBarGUI usb;
	public LibraryManagerMainScreen managerMain;
	public LibrarianMainScreen librarianMain; 
	public UserMainScreen userMain;
	public User logged_in_user;
	public ClientConsole client;
	JLabel lblConnectedToServer;
	public int userID;
	public control.UserControllers userControl;
	public control.LibrarianControllers librarianControl;
	public control.LibraryManagerControllers managerControl;

	/**
	 * Decide which SideBar to show depending on the specific user's role
	 * @param accessLvl indicates on the logged-in user's role
	 */
	public void showSideBar(String accessLvl){
		switch(accessLvl){
		case "1":{			/*User Login*/
			sb=new UserSideBarGUI(this);
			break;
		}
		case "2":{			/*Librarian Login*/
			sb=new LibrarianSideBarGUI(this);
			break;
		}
		case "3":{			/*Manager Login*/
			sb=new LibraryManagerSideBarGUI(this);
			break;
		}
		}
	getContentPane().add(sb);
	sb.setLocation(800-sb.getWidth(),0);
	}
	/**
	 * Create the main frame.
	 */
	public MainFrame() {
		getContentPane().setBackground(new Color(0, 102, 102));
		setTitle("ORT BRAUDE LIBRARY");
		setMinimumSize(new Dimension(800, 600));
		setMaximumSize(new Dimension(800, 600));
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
	
		lg=new LoginPanel();
		lg.btnLogin.setLocation(20, 155);
		lg.setVisible(false);
		lg.mf=this;
		lg.setBounds(172, 188, 270, 193);
		getContentPane().add(lg);
		lg.setLayout(null);
		
		lblConnectedToServer = new JLabel("Connected to Server");
		lblConnectedToServer.setVisible(false);
		lblConnectedToServer.setForeground(Color.BLUE);
		lblConnectedToServer.setBounds(316, 546, 177, 14);
		getContentPane().add(lblConnectedToServer);
		
		userMain = new UserMainScreen(this);
		librarianMain = new LibrarianMainScreen(this);
		managerMain = new LibraryManagerMainScreen(this);
		userControl = new UserControllers(this);
		librarianControl = new LibrarianControllers(this);
		managerControl = new LibraryManagerControllers(this);
		lg.getRootPane().setDefaultButton(lg.btnLogin);
		conGUI=new ConnectionGUI();
		conGUI.setBounds(198, 41, 220, 98);
		getContentPane().add(conGUI);
		conGUI.mf=this;
		
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(615, 572);
		getContentPane().add(picLabel);
		this.setVisible(true);
	}
	
	

	/**
	 * Receive a specific message from the client and direct it to the appropriate controller
	 * @param msg The Message received from a specific client
	 */
	public void receiveMessage(Object msg){
		@SuppressWarnings("unchecked")
		ArrayList<Object> messages=(ArrayList<Object>) msg;
		System.out.println(messages.get(1).toString());
		
		if(messages.get(0).toString().equals("Login"))
		{
		if((messages.get(1).toString().equals("Fail"))  || (messages.get(1).toString().equals("AlreadyLoggedIn")) )
		{
			lg.loginFailed();
			return;
		}
		
		if ((messages.get(1).toString().equals("OK")))
		{
			lg.lblError.setVisible(false);
			lg.textUserName.setText("");
			lg.textPassword.setText("");
			System.out.println("message recieved 3");
			logged_in_user=(User) messages.get(2);
			lg.loginOK(logged_in_user.role);
		}	
		}
		else if(messages.get(0).toString().equals("LogOut"))
		{
		if((messages.get(1).toString().equals("Success")))
		{
			System.out.println("LOGGED OUT");
			return;
		}
		}
		
		if(messages.get(0).toString().equals("BookSearch"))
		{
			if (messages.get(1).toString().equals("OK"))
			{
                String[] book;

                for(int i=2;i<messages.size();i++)
                {
                	book = (String[]) messages.get(i);
                    String[] bookInfo = {book[0],book[1],book[2],book[3]};
                    userMain.bookSearch.searchTableModel.addRow(bookInfo);
                    librarianMain.searchBook.searchTableModel.addRow(bookInfo);
                }

        }       
        else

                JOptionPane.showMessageDialog(null, "Such book doesnt exists.",
                                "ERROR", JOptionPane.ERROR_MESSAGE);
		}
		
		if(messages.get(0).equals("SearchCopyID"))
		{
			if(messages.get(1).equals("OK"))
			{
				 for (int i = userMain.bookSearch.searchTableModel1.getRowCount() - 1; i >= 0; i--){ 
					 userMain.bookSearch.searchTableModel1.removeRow(i);
					 librarianMain.searchBook.searchTableModel1.removeRow(i);
				 }
				String[] copy;

                for(int i=2;i<messages.size();i++)
                {
                	copy = (String[]) messages.get(i);
                    String[] copyInfo = {copy[0],copy[1]};
					System.out.println(copy[0]);
					System.out.println(copy[1]);
                    userMain.bookSearch.searchTableModel1.addRow(copyInfo);
                    librarianMain.searchBook.searchTableModel1.addRow(copyInfo);
                }
			}
			else
			{
				JOptionPane.showMessageDialog(null, "No Copies for this book", "Program", JOptionPane.ERROR_MESSAGE);
			}
		}
		
		
		/**
		 * send received message to appropriate controller depending on role
		 */
		 switch ((String)messages.get(0))
		  {
		  case ("1"):
		  {
			  userControl.handleMessage((Object)msg);
			  return;
		  }
		  case ("2"):
		  {
			  librarianControl.handleMessage((Object)msg);
			  return;
		  }
		  case ("3"):
		  {
			  managerControl.handleMessage(msg);
			  return;
		  }
		  
		  }	
		
		
	}
	
	
	@SuppressWarnings("unused")
	/**
	 * Run the MainFrame
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame X=new MainFrame();
		RefineryUtilities.centerFrameOnScreen(X);  

	}
}
